package fr.cpe.dp.projet2.model.musicPlayer;

import fr.cpe.dp.projet2.factory.GenericFactory;
import fr.cpe.dp.projet2.model.music.IMusic;
import fr.cpe.dp.projet2.model.musicBuilder.IMusicBuilder;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequencer;

/**
 *
 * @author Benoit
 */
public class ConsoleMusicPlayer extends AbstractMusicPlayer {
    public ConsoleMusicPlayer() throws MidiUnavailableException {
        super();
    }
    
    public ConsoleMusicPlayer(IMusic music) throws MidiUnavailableException {
        super(music);
    }

    @Override
    public void play() throws InvalidMidiDataException {
        super.play();
        System.out.println("Lecture de la musique");
    }

    @Override
    public void pause() {
        super.pause();
        System.out.println("Pause de la musique");
    }

    @Override
    public void stop() {
        super.stop();
        System.out.println("Arrêt de la lecture de la musique");
    }
    
    public static void main(String [] args) {
        try {
            GenericFactory<IMusic> musicFactory = new GenericFactory<>("fr.cpe.dp.projet2.model.music");
            GenericFactory<IMusicBuilder> musicBuilderFactory = new GenericFactory<>("fr.cpe.dp.projet2.model.musicBuilder");
            List<Object> parameters1 = new ArrayList<Object>();
            List<Object> parameters2 = new ArrayList<Object>();
            List<Object> parameters3 = new ArrayList<Object>();
            
            parameters1.add(new File("miley_cyrus-wrecking_ball.mid"));
            parameters2.add(musicBuilderFactory.createObject("RandomBuilder"));
            parameters3.add(musicBuilderFactory.createObject("RangeBuilder"));
            
            IMusic fileMusic = musicFactory.createObject("FileMusic", parameters1);
            IMusic randomMusic = musicFactory.createObject("BuildedMusic", parameters2);
            IMusic rangedMusic = musicFactory.createObject("BuildedMusic", parameters3);
            
            System.out.println("====================================");
            System.out.println("Musique générée Random");
            IMusicPlayer player = new ConsoleMusicPlayer(randomMusic);
            player.play();
            Thread.sleep(5000);
            player.pause();
            Thread.sleep(1000);
            player.play();
            Thread.sleep(3000);
            player.stop();
            System.out.println("====================================");
            System.out.println("Musique générée Gamme");
            player = new ConsoleMusicPlayer(rangedMusic);
            player.play();
            Thread.sleep(8000);
            player.stop();
            System.out.println("====================================");
            System.out.println("Musique lue depuis un fichier");
            player = new ConsoleMusicPlayer(fileMusic);
            player.play();
            Thread.sleep(5000);
            player.pause();
            Thread.sleep(1000);
            player.play();
            Thread.sleep(3000);
            player.stop();
        } catch (MidiUnavailableException | InterruptedException | InstantiationException | InvalidMidiDataException | IllegalAccessException | ClassNotFoundException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException ex) {
            Logger.getLogger(ConsoleMusicPlayer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

	@Override
	public Sequencer getSequencer() {
		return super.getSequencer();
	}
}
